home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / findcust.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  3.3 KB  |  92 lines

  1. //----------------------------------------------------------------------------
  2. //Borland C++Builder
  3. //Copyright (c) 1987, 1998 Borland International Inc. All Rights Reserved.
  4. //----------------------------------------------------------------------------
  5. //---------------------------------------------------------------------------
  6. #include <vcl.h>
  7. #pragma hdrstop
  8.  
  9. #include "FindCust.h"
  10. #include "DM.h"
  11. #include <stdlib.h>
  12.  
  13. //---------------------------------------------------------------------------
  14. #pragma resource "*.dfm"
  15.  
  16. TfmFindCust* fmFindCust;
  17.  
  18. //---------------------------------------------------------------------------
  19. __fastcall TfmFindCust::TfmFindCust(TComponent* Owner)
  20.   : TForm(Owner)
  21. {
  22. }
  23.  
  24. //---------------------------------------------------------------------------
  25. void __fastcall TfmFindCust::ComboBox1Change(TObject *Sender)
  26. {
  27.   // Make the field we're locating in the leftmost field.
  28.   DM2->CustLookup->FieldByName(ComboBox1->Text)->Index = 0;
  29.  
  30.   // In order to use FindNearest, we have to be indexed based on the field
  31.   // we're doing the find in.  If we're using Locate, it doesn't matter what
  32.   // the current index is, so we will set it to company, which is the default
  33.   // in the Customer form.
  34.   if (ComboBox1->Text == "CustNo")
  35.     DM2->CustLookup->IndexName = "";
  36.   else
  37.     DM2->CustLookup->IndexName = "ByCompany";
  38.  
  39.   // Let the user choose whether or not to use a filter if they're going to be
  40.   // searching based on a field that has no index.
  41.   cbUseFilter->Enabled = ComboBox1->Text != "CustNo" && ComboBox1->Text != "Company";
  42.   if (cbUseFilter->Enabled)
  43.     cbUseFilter->SetFocus();
  44.   else
  45.     Edit1->SetFocus();
  46.   Edit1->Text = "";
  47. }
  48. //---------------------------------------------------------------------
  49. void __fastcall TfmFindCust::Edit1Change(TObject *Sender)
  50. {
  51.   // Now, we begin to find the field, as the user types, i.e.,
  52.   // incrementally.  FindNearest is used in the two indexed fields,
  53.   // Company and CustNo; Locate is used for all the non-indexed
  54.   // fields.
  55.   if (ComboBox1->Text == "Company") {
  56.     TVarRec q(Edit1->Text);
  57.     DM2->CustLookup->FindNearest(&q, 0);
  58.   }
  59.   else if (ComboBox1->Text == "CustNo") {
  60.     try {
  61.       TVarRec q(_atold(Edit1->Text.c_str()));
  62.       if (Edit1->Text != "")
  63.         DM2->CustLookup->FindNearest(&q, 0);
  64.     }
  65.     catch (...) {
  66.       throw Exception(Edit1->Text + " is not a valid customer number");
  67.     }
  68.   }
  69.   else { // Some non-indexed field.
  70.  
  71.     Set<TLocateOption,0,1> flags;
  72.     flags << loCaseInsensitive << loPartialKey;
  73.     // Possibly apply the filter so we see only records matching this value.
  74.     bool found;
  75.     found = DM2->CustLookup->Locate(ComboBox1->Text,
  76.                                     Edit1->Text,
  77.                                     flags);
  78.  
  79.     if (found && cbUseFilter->Checked) {
  80.       // Get an expression such as State = 'CA'.
  81.       DM2->CustLookup->Filter = ComboBox1->Text + " = '" + Edit1->Text + "'";
  82.       DM2->CustLookup->Filtered = true;
  83.       if (DM2->CustLookup->RecordCount == 0) //Filter is possibly too restrictive
  84.         DM2->CustLookup->Filter = ComboBox1->Text + " >= '" + Edit1->Text + "'";
  85.     }
  86.     else
  87.       DM2->CustLookup->Filtered = false;
  88.   }
  89.   DM2->CustLookup->Refresh();
  90. }
  91. //---------------------------------------------------------------------------
  92.